import cv2 as cv
import numpy as np

cap = cv.VideoCapture(0)

while (1):
    _, frame = cap.read()

    # 转换颜色空间
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    # 设置颜色范围
    lower_blue = np.array([110, 45, 50])
    upper_blue = np.array([130, 255, 255])
    # 过滤画面，保留需要的部分
    mask = cv.inRange(hsv, lower_blue, upper_blue)
    # 从原图中获取需要的部分
    res = cv.bitwise_and(frame, frame, mask=mask)

    cv.imshow('frame', frame)
    cv.imshow('mask', mask)
    cv.imshow('res', res)
    k = cv.waitKey(5) & 0xFF
    if k == 27:
        break
cv.destroyAllWindows()